#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();return x*f;}
inline void write(int x){if(x<0)putchar('-'),x=-x;if(x>=10)write(x/10);putchar(x%10+'0');}
int a[200005],dp[105][105][105],dp1[200005][11][11];
void main1(){
    int n=read(),maxn=0,ans=0;
    for(int i=1;i<=n;++i){
        a[i]=read();
        maxn=max(maxn,a[i]);
    }
    if(maxn<=10){
        memset(dp1,-0x3f,sizeof(dp1));
    }else{
        memset(dp,-0x3f,sizeof(dp));
    }
    for(int i=1;i<=n;++i){
        if(maxn<=10){
            dp1[i][0][a[i]]=0;
        }else{
            dp[i][0][a[i]]=0;
        }
    }
    for(int i=1;i<=n;++i){
        for(int j=0;j<=maxn;++j){
            if(maxn<=10){
                dp1[i][j][a[i]]=max(dp1[i][j][a[i]],dp1[i-1][j][a[i-1]]+(a[i]==a[i-1])*a[i]);
                dp1[i][a[i-1]][a[i]]=max(dp1[i][a[i-1]][a[i]],dp1[i-1][j][a[i-1]]+(a[i]==j)*a[i]);
            }else{
                dp[i][j][a[i]]=max(dp[i][j][a[i]],dp[i-1][j][a[i-1]]+(a[i]==a[i-1])*a[i]);
                dp[i][a[i-1]][a[i]]=max(dp[i][a[i-1]][a[i]],dp[i-1][j][a[i-1]]+(a[i]==j)*a[i]);
            }
        }
    }
    for(int i=0;i<=maxn;++i){
        if(maxn<=10){
            ans=max(ans,dp1[n][i][a[n]]);
        }else{
            ans=max(ans,dp[n][i][a[n]]);
        }
    }
    write(ans);
    return;
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t=read();
    while(t--){
        main1();
        putchar('\n');
    }
    return 0;
}
